package com.oracle.lingshangejia.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.DataBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;

import java.util.HashMap;
//出
public class RollOutActivity extends BaseActivity implements View.OnClickListener {

    private InputMethodManager manager;
    private RelativeLayout re_back;
    private RelativeLayout re_date;
    private TextView tv_date;
    private EditText et_out_unit; //转出单位
    private EditText et_out_yx;//初步印象
    private EditText et_out_xbs;//现病史
    private EditText et_out_jws;//既往史
    private EditText et_out_zlg;//治疗经过
    private EditText et_out_zz_doctor;//转诊医生
    private EditText et_out_phone;//电话
    private EditText et_out_jg;//当前机构
    private RelativeLayout rl_out_ok;
    private int jmid;
    private int jzid;
    @Override
    public int intiLayout() {
        return R.layout.activity_roll_out;
    }

    @Override
    public void initView() {
        jmid=getIntent().getIntExtra("jmid",0);
        jzid=getIntent().getIntExtra("jzid",0);
        manager =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        re_back = findViewById(R.id.re_back);
        re_back.setOnClickListener(this);
        et_out_jg = findViewById(R.id.et_out_jg);
        et_out_unit=findViewById(R.id.et_out_unit);
        et_out_yx=findViewById(R.id.et_out_yx);
        et_out_xbs=findViewById(R.id.et_out_xbs);
        et_out_jws=findViewById(R.id.et_out_jws);
        et_out_zlg=findViewById(R.id.et_out_zlg);
        et_out_zz_doctor=findViewById(R.id.et_out_zz_doctor);
        et_out_phone=findViewById(R.id.et_out_phone);
        rl_out_ok=findViewById(R.id.rl_out_ok);
        rl_out_ok.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {

        if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
            manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }

        switch (v.getId()){

            //返回键
            case R.id.re_back:
                finish();
                break;
            case R.id.rl_out_ok:
                String unit=et_out_unit.getText().toString().trim();
                if(TextUtils.isEmpty(unit)){
                    Toast.makeText(RollOutActivity.this,"请输入转出单位",Toast.LENGTH_SHORT).show();
                    return;
                }
                String cbyx=et_out_yx.getText().toString().trim();
                if(TextUtils.isEmpty(cbyx)){
                    Toast.makeText(RollOutActivity.this,"请输入初步印象",Toast.LENGTH_SHORT).show();
                    return;
                }
                String xbs=et_out_xbs.getText().toString().trim();
                if(TextUtils.isEmpty(xbs)){
                    Toast.makeText(RollOutActivity.this,"请输入现病史",Toast.LENGTH_SHORT).show();
                    return;
                }
                String jws=et_out_jws.getText().toString().trim();
                if(TextUtils.isEmpty(jws)){
                    Toast.makeText(RollOutActivity.this,"请输入既往史",Toast.LENGTH_SHORT).show();
                    return;
                }
                String jg=et_out_zlg.getText().toString().trim();
                if(TextUtils.isEmpty(jg)){
                    Toast.makeText(RollOutActivity.this,"请输入治疗经过",Toast.LENGTH_SHORT).show();
                    return;
                }
                String doctors=et_out_zz_doctor.getText().toString().trim();
                if(TextUtils.isEmpty(doctors)){
                    Toast.makeText(RollOutActivity.this,"请输入转诊医生",Toast.LENGTH_SHORT).show();
                    return;
                }
                String pp=et_out_phone.getText().toString().trim();
                if(TextUtils.isEmpty(pp)){
                    Toast.makeText(RollOutActivity.this,"请输入联系电话",Toast.LENGTH_SHORT).show();
                    return;
                }
                String date=et_out_jg.getText().toString().trim();
                if(TextUtils.isEmpty(date)){
                    Toast.makeText(RollOutActivity.this,"请输入当前机构名称",Toast.LENGTH_SHORT).show();
                    return;
                }
                getNetWork(unit,cbyx,xbs,jws,jg,doctors,pp,date);
                break;
        }
    }

    private void getNetWork(String unit, String cbyx, String xbs, String jws, String jg, String doctors, String pp, String date) {
        int ysid= (int) SpUtils.get(RollOutActivity.this,"ysid",0);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("jmId",jmid);
        map.put("jzId",jzid);
        map.put("zcjgmc",unit);
        map.put("cbyx",cbyx);
        map.put("xbs",xbs);
        map.put("jws",jws);
        map.put("jljg",jg);
        map.put("zzys_id",ysid);
        map.put("dqjgmc",doctors);
        map.put("ysdh",pp);
        OkHttpUtils.getNetWork(RollOutActivity.this, Constant.baseurl+"online/jzzz/sxzzzc",map,handler);
    }
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:  //成功S
                    String s= (String) msg.obj;
                    Gson gson=new Gson();
                    DataBean bean=gson.fromJson(s, DataBean.class);
                    if(bean.getCode()==0){
                        Toast.makeText(RollOutActivity.this,"转出成功",Toast.LENGTH_SHORT).show();
                        finish();
                    }else{
                        Toast.makeText(RollOutActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(RollOutActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(RollOutActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(RollOutActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN){

            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}
